
Page({
  data: {
    oldaddress:"",  // 身份证号
    oldphone:"",    // 设备码
    region:[],
    navtop:{},
    openid:"",
    lrsfz:"",
    arr:[],
    id:"",
    sfz:""
  },
  onLoad(){

    // 导航栏
    // 1.获取屏幕可使用宽度
    let windowWidth = wx.getSystemInfoSync().windowWidth;
    // 2.获取状态栏高度
    const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
    //3.获取胶囊按钮位置信息
    const menuButton = wx.getMenuButtonBoundingClientRect();
    this.setData({
      navtop:menuButton.top
    })
  },
  onShow(){
    const app=getApp()
    this.someFunction()
    this.someFunction1()
    this.setData({
    sfz:app.globalData.baocunshenfenzheng
    })


  },
  async someFunction() {
    await new Promise((resolve) => {
      this.setData({
        openid:getApp().globalData.openid,
      }, resolve);
    });
    return true
  },

  async someFunction1() {
    const wait1=await this.someFunction()
    wx.request({
      url: 'https://dj.ifankui.com/api/usr/getProfile',
      method:"POST",
      data:{
        openid:this.data.openid
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success:(res)=>{
        this.setData({
          oldaddress:res.data.data.lrsfz,
        },()=>{
          wx.request({
            url: 'https://dj.ifankui.com/api/usr/deviceList',
            method:"POST",
            data:{
              openid:this.data.openid
            },
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success:(res)=>{
              console.log(res);
              if(res.data.data.length==0){
                wx.showToast({
                  title: '请绑定设备',
                  icon: 'error'
                });
              }
              this.setData({
                arr:res.data.data,
              })
              
            }
          
          })
        })
      }
    })
  },



   //返回上一个页面
   gethui() {
    wx.switchTab({
      url: '/pages/Member/Member'
    })
},
  bindRegionChange: function (e) {
    console.log(e.detail.value)
    console.log(e.detail.code)
    this.setData({
      region: e.detail.value
    })
  },
  gitfor(){
    wx.switchTab({
      url: '/pages/Member/Member',
    })
  },
  oldaddress(e){
    const app=getApp()
    this.setData({
      oldaddress:e.detail.value
    })
    app.globalData.baocunshenfenzheng=e.detail.value
  },

  oldphone(e){
    this.setData({
      oldphone:e.detail.value
    })
  },
  shanchu(e){
    this.data.arr.splice(e.currentTarget.dataset.index,1)
    this.setData({
      arr:this.data.arr
    })
    this.setData({
      id:e.currentTarget.dataset.id
    })
    wx.request({
      url: 'https://dj.ifankui.com/api/usr/deviceUnbind',
      method:"POST",
      data:{
        openid:this.data.openid,
        device_id:this.data.id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success:(res)=>{
        console.log(res);
      }
    })
    wx.showToast({
      title: '删除成功',
      icon: 'success'
    });
  },
  formSubmit: function(e) {
    console.log(e);
    const { sbm, sfz } = e.detail.value;
    if (!sbm.trim() ) {
      wx.showToast({
        title: '设备码不能为空',
        icon: 'none'
      });
      return false; 
    }
    function isValidString(str) {
      const regex = /^[a-zA-Z0-9]+$/;
      return regex.test(str);
    }
    if(!isValidString(this.data.oldphone)){
      console.log(isValidString(!this.data.oldphone));
      wx.showToast({
        title: '请输入正确的设备码',
        icon: 'none'
      });
      return false; 
    }
    if(this.data.arr.length!=0){
      wx.showToast({
        title: '已存在绑定设备',
        icon: 'none'
      });
      return false;
    }
    if ( !sfz.trim()) {
      wx.showToast({
        title: '身份证号不能为空',
        icon: 'none'
      });
      return false; 
    }
  
    // 校验身份证号
    if (!/^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(sfz)) {
      wx.showToast({
        title: '身份证号格式不正确',
        icon: 'none'
      });
      return false; 
    }

    wx.request({
      url: 'https://dj.ifankui.com/api/usr/deviceBind',
      method:'POST',
      data:{
        openid:this.data.openid,
        sfz:this.data.oldaddress,
        device_code:this.data.oldphone
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success:(res)=>{
        wx.showToast({
            title: res.data.msg,
            icon: 'success'
        });
      }
    })
    this.setData({
        oldphone:""
    },()=>{
      this.someFunction1()
    })
    
  },
  onScanCode: function() {
        wx.scanCode({
          success: (res) => {
            // 扫码成功后，res.result 是扫码结果
            console.log(res.result);
            let newdevice = res.result
            let result = newdevice.split(",");
            console.log(result);
            wx.showToast({
              title: '扫描成功',
              icon: 'success',
              duration: 2000
            });
            this.setData({
              oldphone:result[0],
              sfz:result[1]
            })
          },
          fail: (err) => {
            // 扫码失败
            console.error('扫描失败:', err);
            wx.showToast({
              title: '扫描失败',
              icon: 'none',
              duration: 2000
            });
          }
        });
      }
});